package com.etime.perfectschool.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.perfectschool.mapper.UserMapper;
import com.etime.perfectschool.pojo.User;
import com.etime.perfectschool.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    //获取我的全部新闻
    @GetMapping("/getMyAllSchoolNews")
    public Page<User> getMyAllSchoolNews(int id) {
        return userService.getMyAllSchoolNews(id, 1, 5);
    }

    //获取我的全部活动
    @GetMapping("/getMyAllschoolActivies")
    public Page<User> getMyAllschoolActivies(int id) {
        return userService.getMyAllSchoolNews(id, 1, 5);
    }

    //判断是否有改账号
    @GetMapping("/getUserByName")
    public boolean getUserByName(String name) {
        return userService.getUserByName(name);
    }

    //  判断用户登录
    @PostMapping("/getUser")
    public User getUser(String name, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("password", password);
        System.out.println(userService.getUser(map));
        return userService.getUser(map);
    }

    // 用户注册
    @PostMapping("/addUser")
    public boolean addUser(MultipartFile file, String name, String password, int age,
                           String gender, String phonenumber, int role) {
        boolean flag = false;
        String path = "D:\\perfectschool1\\src\\main\\resources\\static\\schoolnews";
        File file1 = new File(path);
        try {
            file.transferTo(new File(file1, file.getOriginalFilename()));
            String headPhoto = file.getOriginalFilename();
            User user = new User(0, name, password, age, gender, phonenumber, headPhoto, role, null, null);
            System.out.println(user);
            flag = userService.addUser(user);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }

    // 通过用户id查找用户
    @GetMapping("/getUserById")
    public User getUserById(int id) {
        return userService.getUserById(id);
    }

    @PutMapping("/updateUser")
    public int updateUser(int age, String gender, String phonenumber, int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("age", age);
        map.put("gender", gender);
        map.put("phonenumber", phonenumber);
        map.put("id", id);
        return userService.updateUser(map);
    }

    //获取全部用户
    @GetMapping("/getUserAll")
    public Page<User> getUserAll(int current, int size, String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        Page<User> page = new Page<>(current, size);
        return userMapper.selectPage(page, wrapper);
    }

    //修改用户信息
    @PostMapping("/updateUser")
    public int updateUser(User user) {
        System.out.println(user);
        return userMapper.updateById(user);
    }
}
